<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2006
sirfragalot@users.sourceforge.net

**********************************************************************************/
/*****
* BROWSECREATOR class
*
* Browse creators as a 'tag cloud'
*
* 
$Header: /cvsroot/wikindx/wikindx3/core/browse/BROWSECREATOR.php,v 1.10 2007/07/20 10:38:09 sirfragalot Exp $
*****/
class BROWSECREATOR2
{
// Constructor
	function BROWSECREATOR2($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/html/MISC.php");
		include_once("core/html/HTML.php");
		$this->html = new HTML();
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/browse/BROWSECOMMON.php");
		$this->common = new BROWSECOMMON($this->db);
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
	}
// display creators
	function display()
	{
		$this->template->setVar('heading', $this->messages->text("heading", "browseCreator2"));
		$this->sum = $this->prefix = $this->surname = $this->resourceIds = array();
		$this->getCreators2('id');
		if(empty($this->surname))
		{
			$pString = MISC::p("&nbsp;") . $this->messages->text("misc", "noCreators");
			$this->template->setVar('body', $pString);
			return $this->template->process();
		}
		natcasesort($this->sum);
		natcasesort($this->surname);
		$this->surname = $this->common->paging($this->vars, $this->surname);
		foreach($this->surname as $id => $surname)
		{
			if(!empty($this->prefix) && array_key_exists($id, $this->prefix))
				$this->surname[$id] = $this->prefix[$id] . ' ' . $surname;
		}
		$creatorSum = $this->sum;
		$creatorSurname = $this->surname;
		$bib = $this->bib ? MISC::p($this->bib, "hint") : FALSE;

		$cId = array_key_exists("id", $this->vars) ? $this->vars["id"] : $this->session->getVar("list_ids");
		$resultSet2 = $this->db->select(array('WKX_institution'), array('acronym','name','address','website','country','comment'),
			' WHERE ' . $this->db->formatField('id') . '=' . $this->db->tidyInput($cId) );
		while($row2 = $this->db->loopRecordSet($resultSet2))
		{
			$acronym = $row2['acronym'];
			$name = $row2['name'];
			$address = $row2['address'];
			$website = $row2['website'];
			$country = $row2['country'];
			$comment = $row2['comment'];
		}

		$this->template->setVar('heading', $acronym.'&nbsp;'.$name.'<br>'.$address.'<br>'.$website);
		$pString = $bib . $this->common->linksInfo() . 
			 MISC::pBrowse($this->process($creatorSum, $creatorSurname));

		$this->template->setVar('body', $pString);
		$this->template->setVar('paging', $this->common->pagingLinks("action=browseCreator2"));
		return $this->template->process();
	}
// Process and display results
	function process($creatorSum, $creatorSurname)
	{
		$lowestSum = current($creatorSum);
		$highestSum = end($creatorSum);
		foreach($this->surname as $id => $name)
		{

//			$name = str_replace(' ', "&nbsp;", $name);
			$name = $this->html->codeToHtml($name);
			//$colour = $this->common->colourText($lowestSum, $highestSum, $creatorSum[$id]);
			//$size = $this->common->sizeText($lowestSum, $highestSum, $creatorSum[$id]);
			$links[] = MISC::aBrowse($colour, $size, $name, "index.php?action=listCreatorProcess" . 
			htmlentities("&id=" . $id))/* . "&nbsp;[" . $creatorSum[$id] . "]"*/;
		}
		return join("<br> ", $links);
	}
	
// Get creators2 from db
	function getCreators2($institution)
	{
		$cId = array_key_exists("id", $this->vars) ? $this->vars["id"] : $this->session->getVar("list_ids");		
// If this is a different creatorlisting to the previous one, reset the paging counter.
		include_once("core/sql/STATEMENTS2.php");
		$stmt = new STATEMENTS2($this->db);
// NB MySQL bug: http://bugs.mysql.com/bug.php?id=2047 -- append comma temporarily
		$condition = " WHERE (
			FIND_IN_SET(" . $this->db->tidyInput($cId) . ", CONCAT(" . 
				$this->db->formatField('institution1') . ", ',')) OR 
			FIND_IN_SET(" . $this->db->tidyInput($cId) . ", CONCAT(" . 
				$this->db->formatField('institution2') . ", ',')) OR 
			FIND_IN_SET(" . $this->db->tidyInput($cId) . ", CONCAT(" . 
				$this->db->formatField('institution3') . ", ',')) OR 
			FIND_IN_SET(" . $this->db->tidyInput($cId) . ", CONCAT(" . 
				$this->db->formatField('institution4') . ", ',')) OR 
			FIND_IN_SET(" . $this->db->tidyInput($cId) . ", CONCAT(" . 
				$this->db->formatField('institution5') . ", ',')))";
		$recordset = $this->db->select(array('WKX_creator_institution'),
			array('institution1', $institution), $condition);
		while($row = $this->db->loopRecordSet($recordset))
			$this->collate($institution, $row);
		if($bib)
			$this->bib = $this->messages->text("user", "bibliography") . ": " . $bib;
		else if($this->session->getVar('setup_multiUser'))
			$this->bib = $this->messages->text("user", "bibliography") . ": " . 
				$this->messages->text("user", "masterBib");
		else
			$this->bib = FALSE;
	}
	
// Add creators to array and sum totals
	function collate($creator, $row)
	{
		foreach(split(",", $row[$creator]) as $key => $value)
		{
			if($value)
				$creators[] = preg_replace("/{(.*)}/U", "$1", $value);
		}
		if(!isset($creators))
			return FALSE;
		foreach($creators as $creatorId)
		{
			if(!array_key_exists($row['id'], $this->resourceIds))
				$this->resourceIds[$row['id']][] = $creatorId;
			else if(array_search($creatorId, $this->resourceIds[$row['id']]) !== FALSE)
				continue; // this creator already listed for this resource
			else
				$this->resourceIds[$row['id']][] = $creatorId;
			if(!array_key_exists($creatorId, $this->sum))
				$this->sum[$creatorId] = 1;
			else
				++$this->sum[$creatorId];
			$creatorsCondition[] = "'$creatorId'";
		}
		if(!isset($creatorsCondition))
			return;
		$creatorsCondition = array_unique($creatorsCondition);
		$condition = $this->db->formatField('id') . "=" . join(" OR " .
			$this->db->formatField('id') . "=", $creatorsCondition);
		$recordset = $this->db->select(array('WKX_creator'), array("id", "prefix", "surname"), 
			" WHERE " . $condition);
		while($row = $this->db->loopRecordSet($recordset))
		{
			$this->surname[$row['id']] = preg_replace("/{(.*)}/U", "$1", stripslashes($row['surname']));
			$this->prefix[$row['id']] = preg_replace("/{(.*)}/U", "$1", stripslashes($row['prefix']));
		}
		
	}
}
?>